import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:tpbook_flutter/public.dart';

import 'bookshelf_item_view.dart';
import 'bookshelf_header.dart';

class BookshelfScene extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => BookshelfState();
}

class BookshelfState extends State<BookshelfScene> with RouteAware {
  List<Novel> favoriteNovels = [];
  ScrollController scrollController = ScrollController();
  double navAlpha = 1;

  @override
  void initState() {
    super.initState();
    fetchData();

//    scrollController.addListener(() {
//      var offset = scrollController.offset;
//      if (offset < 0) {
//        if (navAlpha != 0) {
//          setState(() {
//            navAlpha = 0;
//          });
//        }
//      } else if (offset < 50) {
//        setState(() {
//          navAlpha = 1 - (50 - offset) / 50;
//        });
//      } else if (navAlpha != 1) {
//        setState(() {
//          navAlpha = 1;
//        });
//      }
//    });
  }

  Future<void> fetchData() async {
    try {
      List<Novel> favoriteNovels = [];
      List<dynamic> favoriteResponse = await Request.get(action: 'bookshelf');
      favoriteResponse.forEach((data) {
        favoriteNovels.add(Novel.fromJson(data));
      });

      setState(() {
        this.favoriteNovels = favoriteNovels;
      });
    } catch (e) {
      Toast.show(e.toString());
    }
  }

  Widget buildActions(Color iconColor) {
    return Row(children: <Widget>[
//      Container(
//        height: kToolbarHeight,
//        width: 44,
//        child: Image.asset('img/actionbar_checkin.png', color: iconColor),
//      ),
      Container(
        height: kToolbarHeight,
        width: 44,
        child: Image.asset('img/icon_edit.png', color: iconColor),
      ),
      SizedBox(width: 15)
    ]);
  }

  Widget buildNavigationBar() {
    return Stack(
      children: <Widget>[
        Positioned(
          right: 0,
          child: Container(
            margin: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
            child: buildActions(SQColor.white),
          ),
        ),
        Opacity(
          opacity: navAlpha,
          child: Container(
            padding: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
            height: Screen.navigationBarHeight,
            color: SQColor.white,
            child: Row(
              children: <Widget>[
                SizedBox(width: 20),
//                Expanded(
//                  child: Container(
//                    decoration: BoxDecoration(
//                      color: SQColor.grayf2,
//                      borderRadius: BorderRadius.all(Radius.circular(20)),
//
//                    ),
//                    child: Row(
//                    children: <Widget>[
//                      Icon(Icons.search),
//                      TextField(
//                        textInputAction: TextInputAction.search,
//                        maxLines: 1,//最大行数
//                        autofocus: false,//是否自动对焦
//                        textAlign: TextAlign.center,//文本对齐方式
//                        style: TextStyle(fontSize: 14.0, color: SQColor.gray9),//输入文本的样式
//                        inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],//允许的输入格式
//                        decoration: InputDecoration(
//                          hintText: '搜索书架',
//                        ),
//                      ),
//                    ]
//                    ),
//                  ),
//                ),
                Expanded(
                  child: Theme(
                    data: new ThemeData(
                        primaryColor: SQColor.grayf2, hintColor: SQColor.gray9),
                    child: TextField(
                      textInputAction: TextInputAction.search,
                      maxLines: 1,
                      //最大行数
                      autofocus: false,
                      //是否自动对焦
//                      textAlign: TextAlign.center,
                      //文本对齐方式
                      style: TextStyle(fontSize: 14.0, color: SQColor.gray9),
                      //输入文本的样式
                      inputFormatters: [
                        WhitelistingTextInputFormatter.digitsOnly
                      ],
                      //允许的输入格式
                      decoration: InputDecoration(
                        fillColor: SQColor.grayf2,
                        filled: true,
                        prefixIcon: Icon(Icons.search),
                        contentPadding: EdgeInsets.all(11.0),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: SQColor.grayf2,
                          ),
                          borderRadius: BorderRadius.circular(20.0),
                          gapPadding: 11,
                        ),
                        hintText: '搜索书架',
                      ),
                    ),
                  ),
                ),
                buildActions(SQColor.darkGray),
              ],
            ),
          ),
        )
      ],
    );
  }

  Widget buildFavoriteView() {
    if (favoriteNovels.length <= 1) {
      return Container();
    }

    List<Widget> children = [];
    var novels = favoriteNovels.sublist(1);
    novels.forEach((novel) {
      children.add(BookshelfItemView(novel));
    });
    var width = (Screen.width - 15 * 2 - 24 * 2) / 3;
    children.add(GestureDetector(
      onTap: () {
        eventBus.emit(EventToggleTabBarIndex, 1);
      },
      child: Container(
        color: SQColor.paper,
        width: width,
        height: width / 0.75,
        child: Image.asset('img/bookshelf_add.png'),
      ),
    ));
    return Container(
      padding: EdgeInsets.fromLTRB(15, 20, 15, 15),
      child: Wrap(
        spacing: 23,
        children: children,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: SQColor.white,
      body: AnnotatedRegion(
        value: navAlpha > 0.5
            ? SystemUiOverlayStyle.dark
            : SystemUiOverlayStyle.light,
        child: Stack(children: [
          RefreshIndicator(
            onRefresh: fetchData,
            child: ListView(
              padding: EdgeInsets.only(top: 0),
              controller: scrollController,
              children: <Widget>[
//                 Container(),
                favoriteNovels.length > 0
                    ? BookshelfHeader(favoriteNovels[0])
                    : Container(),
                buildFavoriteView(),
              ],
            ),
          ),
          buildNavigationBar(),
        ]),
      ),
    );
  }
}
